﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPWeb.Class;
using KPWeb.Data;
using KPWeb.Function;
using MSExcel = Microsoft.Office.Interop.Excel;



namespace KPWeb.ListyObecnosci
{

    public partial class PracownicyPlanyNieobecnosciExcel : KPPage
    {
        private readonly string[] MonthNames = { "Styczeń", "Luty", "Marzec", "Kwiecień", "Maj", "Czerwiec", "Lipiec", "Sierpień", "Wrzesień", "Październik", "Listopad", "Grudzień" };
        private short RokPlany
        {

            get
            {
                if (ViewState["RokPlany"] == null)
                    ViewState["RokPlany"] = DateTime.Now.Year;
                return short.Parse(ViewState["RokPlany"].ToString());
            }
            set
            {
                ViewState["RokPlany"] = value;

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Error1.Visible = false;
            fError.Visible = false;
            if (!IsPostBack)
            {

                dGrupa.Items.Clear();
                dGrupa.Items.Add(new ListItem() { Text = Stale.wybierzGrupe, Value = "0" });
                foreach (vKP_JednostkiGrupy item in Model.GetGrupyDlaFirmy(int.Parse(Current.FirmaID)))
                {
                    dGrupa.Items.Add(new ListItem()
                    {
                        Text = item.GrupaOpis + " (" + item.GrupaID.ToString() + ")",
                        Value = item.GrupaID.ToString()
                    });
                }


                //RokPlany = short.Parse(Request["value"]);
                //RokPlany += short.Parse(DateTime.Now.Year.ToString());


                dYear.Items.Clear();
                foreach (short item in Kalendarz.GetYearGdzieWpisanoSwieta())
                {
                    this.dYear.Items.Add(item.ToString());
                }

                dYear.SelectedValue = RokPlany.ToString();

                //string[] MonthNames
                dMonth.Items.Clear();
                for (int i = 0; i < 12; i++)
                {
                    dMonth.Items.Add(new ListItem()
                     {
                         Text = MonthNames[i],
                         Value = (i + 1).ToString()
                     });

                    //   this.dMonth.Items.Add(MonthNames[i]);
                }

                dYear.SelectedValue = RokPlany.ToString();
                dMonth.SelectedValue = DateTime.Now.Month.ToString();
            }

        }
        protected int PracownicyPlanyNieobecnosciWyswietlExcel(byte typ, string grupa, int year, int month1, int month2) //0 bez tytułów, 1 ztytułami
        // 0 OK
        {
            int iTemp = 0;
            //int[][] formatRow = new int[3][];
            //formatRow[0] = new int[2] { 1, 12 };
            //formatRow[1] = new int[2] { 4, 125 };
            //formatRow[2] = new int[2] { 5, 10 };
            //   string temp = "";
        

            MSExcel.Application excelApp = new MSExcel.Application();
            string filePath = Server.MapPath("~/EXCEL/SzUrlopyPlany.xls");


            //int[] intArray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            //Range rng = excelApp.get_Range("A1", "J1");
            //rng.Value = intArray;

            //nowy otwórz
            //  MSExcel.Workbook newWorkbook = excelApp.Workbooks.Add();
            //istniejacy otwórz
            MSExcel.Workbook excelWorkbook = excelApp.Workbooks.Open(filePath,
                    0, true, 5, "", "", false, MSExcel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);

            //Opis parametrów funkcji   //msdn.microsoft.com/en-us/library/bb179167%28v=office.12%29.aspx
            //  WorkBooks.open(string Filename, object UpdateLinks, object ReadOnly, object Format, object Password,
            //object WriteResPassword, object ReadOnlyRecommend, object Origin, object Delimiter,
            //object Editable, object Notify, object Converter, object AddToMru, object Local, object CorruptLoad )

            MSExcel.Sheets excelSheets = excelWorkbook.Worksheets;

            string currentSheet = "ZestawienieUrlopy";
            MSExcel.Worksheet excelWorksheet = (MSExcel.Worksheet)excelSheets.get_Item(currentSheet);

            MSExcel.Range excelCell ;
            MSExcel.Range excelCellTarget;
            MSExcel.Range excelCellSource;
            //f,r ad,2,3
            excelCell = (MSExcel.Range)excelWorksheet.get_Range("F2", "F2");
            excelCell.Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
            excelCell.Offset[1, 0].Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Offset[1, 0].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue);

            excelCell = (MSExcel.Range)excelWorksheet.get_Range("R2", "R2");
            excelCell.Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Salmon);
            excelCell.Offset[1, 0].Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Offset[1, 0].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.MediumTurquoise);
            
            excelCell = (MSExcel.Range)excelWorksheet.get_Range("AD1", "AD1");
            excelCell.Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.MediumSpringGreen);
            excelCell.Offset[1, 0].Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Offset[1, 0].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LimeGreen);
            excelCell.Offset[2, 0].Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
            excelCell.Offset[2, 0].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.GreenYellow);
         
            
            excelCell = (MSExcel.Range)excelWorksheet.get_Range("C1", "C1");
            vKP_JednostkiFirmy item1 = Model.GetJednostkaFirma(int.Parse(KPPage.Current.JednostkaID));
            excelCell.Value = item1.JednostkaNazwa;
            vKP_Swieta[] ListaSwiat; 

            for (int iMonth = month1; iMonth < month2; iMonth++)
            {
                ListaSwiat = Kalendarz.GetSwieta(year, iMonth);

                int lastDayMonth = FunctionGlobal.IleDniMiesiac(year, iMonth);


                excelCell = (MSExcel.Range)excelWorksheet.get_Range("C4", "C4").Offset[iTemp, 0];
                excelCell.Value = MonthNames[iMonth - 1] + " / " + year.ToString();
                excelCell.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Navy);

                DateTime d = new DateTime();
                for (int i = 0; i < lastDayMonth; i++)
                {
                    excelCell = (MSExcel.Range)excelWorksheet.get_Range("G5", "G5").Offset[iTemp, i];
                    excelCell.Value = (i + 1).ToString();

                }
                for (int i = 0; i < lastDayMonth; i++)
                {
                    d = new DateTime(year, iMonth, i + 1);
                    excelCell = (MSExcel.Range)excelWorksheet.get_Range("G6", "G6").Offset[iTemp, i];
                    excelCell.Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
                  
                    if (d.DayOfWeek == DayOfWeek.Saturday)
                    {
                        //  excelCell.Borders[MSExcel.XlBordersIndex.xlDiagonalUp].LineStyle = MSExcel.XlLineStyle.xlContinuous;
                        excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.MediumSpringGreen);
                    }
                    if (d.DayOfWeek == DayOfWeek.Sunday)
                    {
                        //  excelCell.Borders[MSExcel.XlBordersIndex.xlDiagonalUp].LineStyle = MSExcel.XlLineStyle.xlContinuous;
                        excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LimeGreen);
                    }
                }

                foreach (vKP_Swieta sw in ListaSwiat)
                {
                    excelCell = (MSExcel.Range)excelWorksheet.get_Range("G6", "G6").Offset[iTemp, sw.day - 1];
                    excelCell.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.GreenYellow);
                    //  excelCell.Borders[MSExcel.XlBordersIndex.xlDiagonalUp].LineStyle = MSExcel.XlLineStyle.xlContinuous;
                }
                //for (int i = lastDayMonth; i < 31; i++)
                //{
                //    excelCell = (MSExcel.Range)excelWorksheet.get_Range("C" + (i + 6).ToString(), "L" + (i + 6).ToString());

                //    excelCell.Borders.LineStyle = MSExcel.XlLineStyle.xlContinuous;
                //    excelCell.Borders[MSExcel.XlBordersIndex.xlDiagonalUp].LineStyle = MSExcel.XlLineStyle.xlContinuous;

                //}

                //int MaxNrStrony = Model.GetListaObecnosciMaxStrona( int.Parse(Current.JednostkaID)) - 1;
                //for (int i1 = 0; i1 < MaxNrStrony; i1++)
                //{


                //    iAll += dlObecnosci;
                //    excelCellSource = (MSExcel.Range)excelWorksheet.get_Range("A1", "L" + (dlObecnosci).ToString());
                //    excelCellTarget = (MSExcel.Range)excelWorksheet.get_Range("A" + (iAll + 1).ToString(), "L" + (iAll + dlObecnosci).ToString());

                //    excelCell.Copy(excelCellTarget);
                //    for (int i = 0; i < formatRow.Length; i++)
                //    {
                //        ((MSExcel.Range)excelWorksheet.Rows[iAll + formatRow[i][0], Missing.Value]).RowHeight = formatRow[i][1];

                //    }

                //    excelCell = (MSExcel.Range)excelWorksheet.get_Range("B4", Type.Missing);

                //}

                Model model = new Model();


                excelCellSource = (MSExcel.Range)excelWorksheet.get_Range("G6", "AK6").Offset[iTemp, 0];
                excelCell = (MSExcel.Range)excelWorksheet.get_Range("C6", "C6");
                int iInic = 0;
                foreach (vKP_OsobyListaObecnosci item in model.GetListaObecnosci(int.Parse(Current.JednostkaID), int.Parse(grupa)))
                {
                    if (iInic > 0)
                    {
                        excelCellTarget = (MSExcel.Range)excelWorksheet.get_Range("G6", "AK6").Offset[iTemp, 0];
                        excelCellSource.Copy(excelCellTarget);
                    }
                    foreach (KP_PlanyUrlopow iUr in Model.GetPlanowaneUrlopy(item.OsobaID, year, iMonth))
                    {
                        excelCell.Offset[iTemp, iUr.Date.Day + 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                    }
                    excelCell.Offset[iTemp, 0].Value =
                              (typ == 1 ? item.Tytul.ToLower() : "") + "\r\n" + FunctionGlobal.UppercaseFirstWords(item.NazwiskoImie);
                    excelCell.Offset[iTemp, 3].Value = (iInic + 1).ToString();
                    iInic += 1;
                    iTemp += 1;

                }
               
                //int l = 0;
                //for (int i = 0; i < iTemp; i++)
                //{
                //    for (int j = 0; j < liczbaPozycjiNaStronie && l < iTemp; j++)
                //        if (excelCell.Offset[dlObecnosci * (i), j + 1].Value != null)
                //        {
                //            l += 1;
                //            excelCell.Offset[dlObecnosci * (i) + 1, j + 1].Value = l.ToString();

                //        }
                //}

                iTemp += 4;
            }

            excelApp.Visible = true;
            return 0;
        }




        protected void ListaObecnosciView_Click(object sender, System.EventArgs e)
        {
            if (dGrupa.SelectedItem.ToString() == Stale.wybierzGrupe)
            {
                Error1.Visible = true;
                fError.Visible = true;
                return;
            }
            int iTemp = PracownicyPlanyNieobecnosciWyswietlExcel(0, dGrupa.SelectedValue, int.Parse(dYear.SelectedValue), int.Parse(dMonth.SelectedValue),13);
        }
        protected void ListaObecnosciViewTytul_Click(object sender, System.EventArgs e)
        {
            if (dGrupa.SelectedItem.ToString() == Stale.wybierzGrupe)
            {
                Error1.Visible = true;
                fError.Visible = true;
                return;
            }
            int iTemp = PracownicyPlanyNieobecnosciWyswietlExcel(1, dGrupa.SelectedValue, int.Parse(dYear.SelectedValue), int.Parse(dMonth.SelectedValue),13);
        }

        protected void ListaObecnosciBezNazwisk_Click(object sender, System.EventArgs e)
        {

            int iTemp = PracownicyPlanyNieobecnosciWyswietlExcel(2, dGrupa.SelectedValue, int.Parse(dYear.SelectedValue), int.Parse(dMonth.SelectedValue),13);
        }
    }
}